home *** CD-ROM | disk | FTP | other *** search
- Path: chronicle.mti.sgi.com!austern
- From: Nico Josuttis <nico@bredex.de>
- Newsgroups: comp.std.c++
- Subject: FOR comp.std.c++: problems with I/O exceptions
- Date: 18 Mar 1996 09:50:37 PST
- Organization: -
- Approved: austern@isolde.mti.sgi.com
- Message-ID: <199603181627.RAA08990@bredex.bredex.de>
- NNTP-Posting-Host: isolde.mti.sgi.com
- X-Original-Date: Mon, 18 Mar 96 17:27:22 +0100
- X-Authentication-Warning: bredex.bredex.de: Host localhost didn't use HELO protocol
- X-Mts: smtp
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBVAwUBMU2iiUy4NqrwXLNJAQFt1wIAvwIqt1x9jYYpmFoq6cxMxbkcbLO5QAIG
- 8SZMAeoBu0T6YVJq796W4rokhNWTD8wFQssNvFpQL41J1a8HW5zQtw==
- =n7gZ
- Originator: austern@isolde.mti.sgi.com
-
- Hi,
- Trying the new feature of throwing stream exceptions
- i wrote the following program:
-
- ------------------------ snip ----------------------
- #include <iostream>
- #include <cstdlib>
- using namespace std;
-
-
- /* process and print sum of integer read from cin
- */
- int main()
- {
-
- cin.exceptions (ios::failbit | ios::badbit);
-
- try {
- int value, sum;
-
- /* while not EOF
- * read and add value
- */
- sum = 0;
- #ifdef VERSION1
- while (cin >> ws) {
- cin >> value;
- #else
- while (cin >> value) {
- #endif
- sum += value;
- }
-
- cout << "sum: " << sum << endl;
-
- }
- catch (ios::failure error) {
- cerr << "Error: " << error.what() << endl;
- exit (EXIT_FAILURE);
- }
- }
- ------------------------ snip ----------------------
-
- But it didn't work as I expected, because also on EOF
- I ALWAYS got the failbit exception!
- And that happened in both versions.
- After some investigation I found the problem and was surprised,
- because the compiler and library I used had no bug.
- Its a feature, which we should change IMHO.
-
- The problem is, that istream::ipfx() always calls setstate(failbit),
- if after any preparation good() is false.
- So even if only the eofbit is set (due to the skip of whitespace),
- the failbit is also set.
- I think, therefore we must change the specification of ixpf()
- to call setstate(eofbit) if eof() is true and setstate(failbit)
- if any other problem occurs.
-
- But even if that would be fixed, i would have problems implementing
- a simple filter like:
- char c;
- while (cin.get(c)) {
- cout.put(c);
- }
- With the actual specification i see no chance to get no exception on
- EOF but an exception for any failure.
- Therefore perhaps a deeper change in the specification would be
- necessary, namely
- - either don't set failbit if eofbit is set
- - or set eofbit on eof and set the failbit with the NEXT try to read data
- In general I need a chance to handle eof without getting
- failbit exceptions.
-
- ONE OTHER POINT I found:
- You might say, that i could catch all exceptions and look at the
- bits.
- BUT i got no information about the reason for I/O exceptions.
- I have only what() to get a
- undefined message, but I think informations about the stream
- or at least about the state of the stream would be senseful
- to process exceptions on EOF different from other problems.
- So for ios_base::failure an additional member like
- state, which is rdstate() of the stream would be very senseful.
-
- For example I'd like to open file and process its data until EOF
- without any exception, but having exceptions for any error
- and the information to handle it properly.
- If for example i open 2 files i have no chance to check
- which file got the problem.
-
- Please correct me if I'am wrong or don't see any simple solution.
- --------
- Nico address: BREDEX GmbH
- email: nico@bredex.de Nicolai Josuttis
- Fallersleber-Tor-Wall 23
- phone: +49 531 24330-0 D-38100 Braunschweig
- fax: +49 531 24330-99 Germany
- --------
- ---
- [ comp.std.c++ is moderated. To submit articles: Try just posting with your
- newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
- comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
- Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
- Comments? mailto:std-c++-request@ncar.ucar.edu
- ]
-